﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletInfo {
    public Vector2 HitFlyEnemyForce;
    public float MaxBulletFlyDiss;
    public Sprite SP;

    public BulletInfo(Vector2 hitForce ,float dis,Sprite sp) {
        this.HitFlyEnemyForce = hitForce;
        this.MaxBulletFlyDiss = dis;
        this.SP = sp;
    }
}
public class WakeUpConfigBullet : WakeUpConfigStartPosAndOri {
    public BulletInfo BI;
    public WakeUpConfigBullet(Vector2 pos,Vector2 ori,BulletInfo info) :base(pos,ori){
        this.BI = info;
    }
}

public class Bullet : PrefabCommonBase
{

    public float BulletSpeedValue = 2;
    public float BulletMaxFlyDis = 1;

    private Vector2 startPos_ = Vector2.zero;

    private Vector2 hitFlyForce = Vector2.zero;
    // Update is called once per frame
    void Update()
    {
        if (this.checkBulletFlyDiss() >= this.BulletMaxFlyDis*this.BulletMaxFlyDis) {
            this.State = EnumItemState.E_SLEEP;
        }
    }

    private float checkBulletFlyDiss() {
        var vec = new Vector2(this.transform.localPosition.x - this.startPos_.x,this.transform.localPosition.y - this.startPos_.y);
        return vec.x * vec.x + vec.y * vec.y;
    }

    //private float oriToDegree(Vector2 vec) {
    //    var d = Math.Acos();
    //}
    public override void SetStatusByConfig(WakeUpConfigBase config)
    {
        WakeUpConfigBullet wc = config as WakeUpConfigBullet;
        if (wc != null) {
            this.transform.localPosition = new Vector3(wc.StartPos.x, wc.StartPos.y, this.transform.localPosition.z);
            startPos_ = wc.StartPos;
            var rig2d = this.GetComponent<Rigidbody2D>();
            if (rig2d != null) {
                wc.Ori = wc.Ori.normalized;
                rig2d.velocity = new Vector2(
                    wc.Ori.x * this.BulletSpeedValue,
                    wc.Ori.y * this.BulletSpeedValue
                    );
                rig2d.SetRotation(MathHelper.OriToDegree(wc.Ori));
            }
            this.BulletMaxFlyDis = wc.BI.MaxBulletFlyDiss;
            this.hitFlyForce = wc.BI.HitFlyEnemyForce;

            var spr = this.GetComponent<SpriteRenderer>();
            spr.sprite = wc.BI.SP;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.contactCount>0) {
            var pos =transform.localPosition;
            var type = collision.gameObject.tag;
            if (type == "Ground") {
                var rig2d = this.GetComponent<Rigidbody2D>();
                if (rig2d != null) {
                    var vec = rig2d.velocity;
                    var oriValue = vec.x > 0 ? 1 : -1;
                    PrefabsMgr.GetInstance().Send(PrefabsMgr.TYPE_HITWALLSPARK, new WakeUpConfigBomb(pos,new Vector2(oriValue,0),0));
                }
            }
            else if (type == "Enemy") {
                var rig2d = this.GetComponent<Rigidbody2D>();
                if (rig2d != null)
                {
                    var vec = rig2d.velocity;
                    var oriValue = vec.x > 0 ? 1 : -1;

                    var actor = collision.gameObject.GetComponent<ActorHuman>();
                    actor.HitByBullet(this.hitFlyForce.x* oriValue);
                    var rig2dEnemy = collision.gameObject.GetComponent<Rigidbody2D>();
                    rig2dEnemy.velocity = new Vector2(rig2dEnemy.velocity.x, rig2dEnemy.velocity.y + this.hitFlyForce.y);

                    PrefabsMgr.GetInstance().Send(PrefabsMgr.TYPE_HITMANSPARK, new WakeUpConfigBomb(pos, new Vector2(oriValue, 0),0));
                }
            }
        }
        this.State = EnumItemState.E_SLEEP;
    }

    protected override void onActive()
    {
        this.gameObject.SetActive(true);
        this.GetComponent<Rigidbody2D>().WakeUp();
    }

    protected override void onSleep()
    {
        this.gameObject.SetActive(false);
        this.GetComponent<Rigidbody2D>().Sleep();
    }
}
